package org.fastsyncer.rest.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.fastsyncer.common.constant.CommonConstant;
import org.fastsyncer.common.entity.RestResult;
import org.fastsyncer.common.util.ApplicationUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 登录验证处理
 * 
 * @ClassName: LoginController
 * @Description: 登录验证，注销
 * @author: YangPei
 * @date: 2017年7月7日 上午10:03:33
 */
@Controller
@RequestMapping(value = "/authenticate")
public class LoginController {

	private static final Logger logger = LoggerFactory.getLogger(LoginController.class);

	@RequestMapping("")
	public String index(ModelMap model) {
		return "login/login";
	}

	/**
	 * 授权确认
	 * 
	 * @Title: authConfirm
	 * @Description: 权限验证
	 * @param response
	 * @param username
	 *            帐号
	 * @param userpwd
	 *            密码
	 * @return: RestResult
	 */
	@RequestMapping(value = "/login", method = { RequestMethod.POST })
	@ResponseBody
	public RestResult login(HttpServletRequest request, @RequestParam(value = "username") String username,
			@RequestParam(value = "userpwd") String userpwd) {
		try {
			if (username.equals("admin") && userpwd.equals("admin")) {
				logger.info("login success!");
				HttpSession session = request.getSession();
				session.setAttribute("user", username);
				return RestResult.restSuccess("login success!");
			}
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage(), e.getClass());
		}
		return RestResult.restFail("帐号或密码错误.");
	}

//	/**
//	 * 登录
//	 * 
//	 * @Title: login
//	 * @Description: 登录验证
//	 * @param response
//	 * @param username
//	 *            帐号
//	 * @param userpwd
//	 *            密码
//	 * @return: RestResult
//	 */
//	@RequestMapping(value = "/login", method = { RequestMethod.POST })
//	public String login(HttpServletRequest request, HttpServletResponse response, ModelMap model, String username,
//			String userpwd) {
//		try {
//			if (username.equals("admin") && userpwd.equals("admin")) {
//				HttpSession session = request.getSession();
//				session.setAttribute("user", username);
//				response.sendRedirect(ApplicationUtil.getKey(CommonConstant.SERVER_CONTEXT));
//				logger.info("login success!");
//			}
//		} catch (Exception e) {
//			logger.error(e.getLocalizedMessage(), e.getClass());
//		}
//		model.addAttribute("error", "帐号或密码错误.");
//		return "login/login";
//	}

	/**
	 * 注销
	 * 
	 * @Title: logout
	 * @Description: 注销登录
	 * @param username
	 *            帐号
	 * @return: RestResult
	 */
	@RequestMapping(value = "/logout", method = { RequestMethod.GET, RequestMethod.POST })
	public void logout(HttpServletRequest request, HttpServletResponse response) {
		try {
			HttpSession session = request.getSession();
			session.removeAttribute("user");
			// 获取系统项目路径
			String context = ApplicationUtil.getKey(CommonConstant.SERVER_CONTEXT);
			context = context.endsWith("/") ? context : context + "/";
			response.sendRedirect(context + "authenticate");
			logger.info("logout success!");
		} catch (Exception e) {
			logger.error(e.getLocalizedMessage(), e.getClass());
		}
	}

}
